/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lj.licenseadapter.slice;

import com.lj.licenseadapter.ResourceTable;
import net.yslibrary.licenseadapter.Library;
import net.yslibrary.licenseadapter.License;
import net.yslibrary.licenseadapter.LicenseAdapter;
import net.yslibrary.licenseadapter.Licenses;
import net.yslibrary.licenseadapter.internal.GitHubLibrary;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayoutManager;
import ohos.agp.components.ListContainer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import ohos.agp.window.service.WindowManager;

/**
 * ex.
 *
 * @since 2021-06-07
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String address = "resources/rawfile/url.text";
    private static final String fabricurl = "resources/rawfile/fabricurl.text";
    private static final String apachcurl = "resources/rawfile/apachurl.text";

    @Override
    public void onStart(Intent intent) {
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(getColor(ResourceTable.Color_colorPrimary));

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        List<Library> licenses = new ArrayList<>();
        GitHubLibrary.setGitcontext(this);
        // These libraries aren't hosted on GitHub (and you chose not to show the license text)
        licenses.add(Licenses.noContent("Android SDK", "Google Inc.",
                getUrl(address)));
        licenses.add(Licenses.noContent("Fabric", "Google Inc.", getUrl(fabricurl)));

        licenses.add(Licenses.fromGitHubApacheV2("JakeWharton/ButterKnife"));
        licenses.add(Licenses.fromGitHubApacheV2("Hdodenhof/CircleImageView"));
        licenses.add(Licenses.fromGitHubApacheV2("Google/Dagger"));
        licenses.add(Licenses.fromGitHubApacheV2("Google/Gson"));
        licenses.add(Licenses.fromGitHubApacheV2("Immutables/Immutables"));
        licenses.add(Licenses.fromGitHubApacheV2("Square/LeakCanary"));
        licenses.add(Licenses.fromGitHubApacheV2("Square/OkHttp"));
        licenses.add(Licenses.fromGitHubApacheV2("Realm/Realm-Java"));
        licenses.add(Licenses.fromGitHubApacheV2("yqritc/RecyclerView-MultipleViewTypesAdapter"));
        licenses.add(Licenses.fromGitHubApacheV2("yshrsmz/LicenseAdapter"));
        licenses.add(Licenses.fromGitHubApacheV2("JakeWharton/ThreeTenABP"));
        licenses.add(Licenses.fromGitHubApacheV2("JakeWharton/Timber"));
        licenses.add(Licenses.fromGitHubApacheV2("GrandCentrix/Tray"));
        licenses.add(Licenses.fromGitHubApacheV2("Twitter/Twitter-Text"));
        licenses.add(Licenses.fromGitHubMIT("Jhy/Jsoup"));
        licenses.add(Licenses.fromGitHubBSD("Bumptech/Glide"));
        licenses.add(Licenses.fromGitHubBSD("Facebook/Stetho"));

        licenses.add(Licenses.fromGitHub("GabrieleMariotti/ChangelogLib",new License.Builder("Apache License 2.0")
                .setUrl(getUrl(apachcurl))
                .build()));
        licenses.add(Licenses.fromGitHubApacheV2("ReactiveX/RxAndroid", "2.x/" + Licenses.FILE_AUTO));
        licenses.add(Licenses.fromGitHubApacheV2("ReactiveX/RxJava", "2.x/" + Licenses.FILE_AUTO));
        licenses.add(Licenses.noLink("Library without a link to the license, like Google Play Services",
                "Author", "License name", "License content"));
        ListContainer list = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        list.setLongClickable(false);
        list.setReboundEffect(false);
        list.setLayoutManager(new DirectionalLayoutManager());
        list.setItemProvider(new LicenseAdapter(this, licenses));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private String getUrl(String address) {
        ohos.global.resource.ResourceManager resManager = this.getResourceManager();
        ohos.global.resource.RawFileEntry rawFileEntry = resManager.getRawFileEntry(address);
        StringBuffer stringBuffer = new StringBuffer();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(rawFileEntry.openRawFile()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            e.getMessage();
        }
        return "";
    }
}
